package com.hongsuan.pplive.mobileAPI.utils;

import java.util.Map;

import com.cnjson.common.mapper.gson.NullStringToEmptyAdapterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class JsonUtils {

	/**
	 * String to Enity
	 * 
	 * @author LLW
	 * @return
	 */
	public static <T> T fromJson(Object obj, Class<T> type) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		return gson.fromJson(obj.toString(), type);
	}

	public static <T> T fromJson(String json, Class<T> type) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		return gson.fromJson(json, type);
	}

	/**
	 * successCallback
	 * 
	 * @return
	 */
	public static String successCallback(Object obj) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		return "successCallback(" + gson.toJson(obj) + ")";
	}

	/**
	 * 通过key获取相应JSON串
	 * @param datas原始串
	 * @param key
	 * @return
	 */
	public static String getData(String datas, String key) {
		Gson gson = getBuilder().create();
		Map<String, Object> map = gson.fromJson(datas, new TypeToken<Map<String, Object>>() {
		}.getType());
		String ret = gson.toJson(map.get(key));
		return ret;
	}
	/**
	 * 把实体对象转为JSON串
	 * @param obj
	 * @return
	 */
	public static String toJson(Object obj) {
		Gson gson = getBuilder().create();
		return gson.toJson(obj);
	}

	/**
	 * 获取GSON builder 对象
	 * @return
	 */
	public static GsonBuilder getBuilder() {
		GsonBuilder gb = new GsonBuilder();
		gb.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
		//然后用上面一行写的gson来序列化和反序列化实体类type
		gb.setDateFormat("yyyy-MM-dd HH:mm:ss");
		//gb.serializeNulls();
		return gb;
	}
 
	/**
	 * 把不规则的字符串替换为规则JSON串
	 * @param json 源JSON串
	 * @return 替换后的JSON串
	 */
	public static String placeJson(String json) {
		String obj = json.replace("\"[", "[").replace("]\"", "]").replace("\\", "").replace("\"{", "{").replace("}\"",
				"}");
		return obj;
	}

	public static String loadJson(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public static void main(String[] args) {
		 
		String videoUrl = "[{\"vid\":\"200012039_5d99fbc503a34300ba61915e687c728f\",\"duration\":\"336\",\"fileName\":\"77021_2016-09-17-12-19-46_2016-09-17-12-25-27\",\"image_url\":\"http://p.qpic.cn/videoyun/0/200012039_5d99fbc503a34300ba61915e687c728f_1/640\",\"playSet\":[{\"vbitrate\":0.0,\"definition\":0.0,\"vheight\":0.0,\"vwidth\":0.0,\"url\":\"http://200012039.vod.myqcloud.com/200012039_5d99fbc503a34300ba61915e687c728f.f0.mp4\"}],\"fileId\":\"14651978969275993443\",\"status\":\"2\"}]";
		int begin = videoUrl.indexOf("playSet");
		int end = videoUrl.indexOf(".mp4")+4;
		String sub= videoUrl.substring(begin, end);
		String subStr = sub.substring(sub.indexOf("http:"));
		System.out.println(subStr);
	}



}
